<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- 表格区域 -->
      <el-table stype="width: 100%;" :data="cateList" border stripe>
        <el-table-column label="序号" type="index" width="100"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <!-- <template v-slot:default="obj"> -->
          <template v-slot="obj">
            <el-button type="primary" size="mini" @click="showEditDialog(obj.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="remove(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog title="添加文章分类" :visible.sync="addVisible" width="35%" @closed="onAddClosed">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">添 加</el-button>
      </span>
    </el-dialog>
    <!-- 修改文章分类的对话框 -->
    <el-dialog title="修改文章分类" :visible.sync="editVisible" width="35%">
      <!-- 修改的表单 -->
      <el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      cateList: [],
      addVisible: false,
      // 修改对话框
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addFormRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
    // 表单的数据对象

    // 表单的验证规则对象
  },
  methods: {
    async initCateList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return this.$message.error('数据请求失败')

      this.cateList = res.data
    },
    // 监听对话框的关闭事件
    onAddClosed() {
      this.$refs.addFormRef.resetFields()
    },
    addCate() {
      // 预验证
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error('请求失败')
        this.$message.success('请求成功')
        this.initCateList()
        this.addVisible = false
      })
    },
    showEditDialog(row) {
      // 展示修改分类的对话框
      this.editForm = Object.assign({}, row)
      this.editVisible = true
    },
    editCate() {
      this.$refs.editFormRef.validate(async valid => {
        console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error('失败了')
        this.$message.success('成功了')
        this.initCateList()
        this.editVisible = false
      })
    },
    async remove(id) {
      if (id === 1 || id === 2) {
        this.$message.error('管理员不允许删除')
        return
      }

      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result === 'cancel') return
      const { data: res } = await this.$http.delete('/my/cate/del', {
        params: { id }
      })
      if (res.code !== 0) return this.$message.error('删除分类失败')
      this.$message.success('成功了')
      this.initCateList()
    }
  },

  created() {
    this.initCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
